package com.chencs.web5.app.controller;

import com.alibaba.fastjson.JSON;
import com.chencs.vo.net.request.ProductListRequest;
import com.chencs.vo.net.request.ProductRequest;
import com.chencs.vo.net.request.PushRequest;
import com.chencs.vo.net.response.ProductListResponse;
import com.chencs.web5.app.service.H5Service;
import com.chencs.web5.model.HProduct;
import framework.model.ResponseOptUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RequestMapping("/")
@RestController
public class H5Controller {

    @Autowired
    H5Service h5Service;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView index(Model model) {

        ProductListRequest request = new ProductListRequest();
        request.setCid("1000");
        request.setTime("time");
        request.setPageNo(0);
        request.setPageSize(10);
        ProductListResponse response = null;
        try {
            response  = JSON.parseObject(h5Service.getProductList(request),ProductListResponse.class);
            System.out.println(response.getProducts().size());
        } catch (Exception e) {
            e.printStackTrace();
        }

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("index");
        model.addAttribute("proList",response.getProducts());
        return modelAndView;
    }


    @RequestMapping(value = "/indexList/{page}", method = RequestMethod.GET)
    public String indexList(@PathVariable("page") String page) {
        ProductListRequest request = new ProductListRequest();
        request.setCid("1000");
        request.setTime("time");
        request.setPageNo(Integer.parseInt(page));
        request.setPageSize(10);
        String retStr = "";
        try {
            retStr = h5Service.getProductList(request);
        } catch (Exception e) {
            retStr = ResponseOptUtil.commonMsgError(ResponseOptUtil.RET_FAIL_MSG);
            e.printStackTrace();
        }
        return retStr;
    }





    @RequestMapping(value = "/product/{id}", method = RequestMethod.GET)
    public ModelAndView info(@PathVariable("id") String id , Model model) {
        System.out.println("id:"+id);

        ProductRequest request = new ProductRequest();
        request.setPid(id);

        System.out.println(h5Service.findProduct(request));
        HProduct product = JSON.parseObject(h5Service.findProduct(request),HProduct.class);


        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("product");

        model.addAttribute("image",product.getNTbkItem().getSmallImages().get(0));
        product.getNTbkItem().getSmallImages().remove(0);
        model.addAttribute("m",product);
        return modelAndView;
    }


    @RequestMapping(value = "/category", method = RequestMethod.GET)
    public ModelAndView category() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("category");
        return modelAndView;
    }


    /**
     * 精选首页
     * @param model
     * @return
     */

    @RequestMapping(value = "/jingxuan", method = RequestMethod.GET)
    public ModelAndView jingxuan(Model model) {
        ProductListRequest request = new ProductListRequest();
        request.setCid("1000");
        request.setTime("time");
        request.setPageNo(0);
        request.setPageSize(10);
        ProductListResponse response = null;
        try {
            response  = JSON.parseObject(h5Service.getProductList(request),ProductListResponse.class);
            System.out.println(response.getProducts().size());
        } catch (Exception e) {
            e.printStackTrace();
        }

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("jingxuan");
        model.addAttribute("proList",response.getProducts());
        return modelAndView;
    }


    /**
     * 精选分页
     * @param page
     * @return
     */
    @RequestMapping(value = "/jingxuanList/{page}", method = RequestMethod.GET)
    public String jingxuanList(@PathVariable("page") String page) {
        ProductListRequest request = new ProductListRequest();
        request.setCid("1000");
        request.setTime("time");
        request.setPageNo(Integer.parseInt(page));
        request.setPageSize(10);
        String retStr = "";
        try {
            retStr = h5Service.getProductList(request);
        } catch (Exception e) {
            retStr = ResponseOptUtil.commonMsgError(ResponseOptUtil.RET_FAIL_MSG);
            e.printStackTrace();
        }
        return retStr;
    }




    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public ModelAndView search() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("search");
        return modelAndView;
    }


    //爆款
    @RequestMapping(value = "/baokuan", method = RequestMethod.GET)
    public ModelAndView baokuan(Model model) {
        PushRequest request = new PushRequest();
        request.setType("100");
        request.setPageNo(0);
        request.setPageSize(10);
        ProductListResponse response = JSON.parseObject(h5Service.topList(request),ProductListResponse.class);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("baokuan");
        model.addAttribute("proList",response.getProducts());
        return modelAndView;
    }


    //爆款分页
    @RequestMapping(value = "/baokuanList/{page}", method = RequestMethod.GET)
    public String baokuanList(@PathVariable("page") String page) {
        PushRequest request = new PushRequest();
        request.setType("100");
        request.setPageNo(Integer.parseInt(page));
        request.setPageSize(10);
        String retStr = "";
        try {
            retStr = h5Service.topList(request);
        } catch (Exception e) {
            retStr = ResponseOptUtil.commonMsgError(ResponseOptUtil.RET_FAIL_MSG);
            e.printStackTrace();
        }
        System.out.println(retStr);
        return retStr;
    }




    //特价首页
    @RequestMapping(value = "/tejia", method = RequestMethod.GET)
    public ModelAndView tejia( Model model) {
        PushRequest request = new PushRequest();
        request.setType("200");
        request.setPageNo(0);
        request.setPageSize(10);

        ProductListResponse response = JSON.parseObject(h5Service.topList(request),ProductListResponse.class);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("tejia");
        model.addAttribute("proList",response.getProducts());
        return modelAndView;
    }


    //特价分页
    @RequestMapping(value = "/tejiaList/{page}", method = RequestMethod.GET)
    public String tejiaList(@PathVariable("page") String page) {
        System.out.println(page);
        PushRequest request = new PushRequest();
        request.setType("200");
        request.setPageNo(Integer.parseInt(page));
        request.setPageSize(10);
        String retStr = "";
        try {
            retStr = h5Service.topList(request);
        } catch (Exception e) {
            retStr = ResponseOptUtil.commonMsgError(ResponseOptUtil.RET_FAIL_MSG);
            e.printStackTrace();
        }
        System.out.println(retStr);
        return retStr;
    }

}
